package com.wpq.netty.sdk;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

public class SimpleChatServerHandler extends SimpleChannelInboundHandler<String> {
    public static ChannelGroup channels =new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception{
        Channel incoming =ctx.channel();
        channels.writeAndFlush("[SERVER]-"+incoming.remoteAddress()+"加入\n");
        channels.add(ctx.channel());
    }
    @Override
    public  void handlerRemoved(ChannelHandlerContext ctx) throws Exception{
        Channel incoming =ctx.channel();
        channels.writeAndFlush("[SERVER]-"+incoming.remoteAddress()+"离开\n");
    }

    /**
     * 每当服务端读到客户端写入信息时，将信息转发给其他客户端的channel
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
     Channel incoming =ctx.channel();
     for (Channel channel:channels){
         if(channel !=incoming){
             channel.writeAndFlush("["+incoming.remoteAddress()+"]"+msg+"\n");
         }else {
             channel.writeAndFlush("[you]"+msg+"\n");
         }
     }
    }
    //服务端监听到客户端活动
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception{
        Channel incoming =ctx.channel();
        System.out.println("simpleChatClient:"+incoming.remoteAddress()+"在线");
    }
    @Override
    public void channelInactive(ChannelHandlerContext ctx ) throws  Exception{
        Channel incoming = ctx.channel();
        System.out.println("simpleChatClient:"+incoming.remoteAddress()+"掉线");
    }
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
        Channel incoming=ctx.channel();
        System.out.println("simpleChatClient:"+incoming.remoteAddress()+"异常");
        //当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}